package threshold;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2023-03-05 19:18
 * @Version 1.0
 */


public class DeleteDuplicates {

     public ListNode deleteDuplicates(ListNode head) {
         if(head.next==null){return head;}
         ListNode p=head,q=head.next,r;
         while(p!=null || q!=null){
             r =p;
             q = p.next;
             while(q!=null){
                 if(q.val==p.val){
                     r.next = q.next;
                     q = r.next;
                 }else {
                     r=q;
                     q = q.next;
                 }
             }
             p = p.next;

         }
         return head;
    }

    /**
     * 值得思考
     * @param head
     * @return
     */
    public ListNode deleteDuplicates1(ListNode head) {

         if(head==null || head.next==null){return null;}
         ListNode p =head;
         while(p.next!=null){
             if(p.val == p.next.val){
                 p.next = p.next.next;
             }else{
                 p = p.next;
             }

         }
         return head;
    }



    public static void main(String[] args) {
        DeleteDuplicates object = new DeleteDuplicates();
        ListNode ls1= new ListNode(1);
        ListNode ls2 = new ListNode(1);
        ListNode ls3 = new ListNode(2);
        ListNode ls4 = new ListNode(3);
        ListNode ls5 = new ListNode(3);
        ls1.next = ls2;ls2.next= ls3;ls3.next=ls4;ls4.next=ls5;
        ListNode listRe = object.deleteDuplicates1(ls1);
        while (listRe!=null){
            System.out.println(listRe.val);
            listRe = listRe.next;
        }
    }
}
